import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

///provider状态管理
class ProviderPage2 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _ProviderPageState();
}

class _ProviderPageState extends State<ProviderPage2> {
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print('====didChangeDependencies');
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => Model1(),
      child: Scaffold(
        appBar: AppBar(
          title: Text('状态管理2'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _AddButtonWidget(),
              // _ShowTextWidget(),
              // Consumer<Model1>(builder: (context, value, child){
              //   return RaisedButton(
              //     onPressed: () => _add(context),
              //     child: Text('add'),
              //   );
              // }),
              _ValueNotifierWidget(),
              _SelectWidget(),
              _ConsumerWidget(),
              _FutureProviderWidget(),
              _ProxyProviderWidget(),
              _StreamProviderWidget(),
            ],
          ),
        ),
      ),
    );
  }

  _add(BuildContext context) {
    Provider.of<Model1>(context, listen: false).count++;
  }
}

class _StreamProviderWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 24.0),
      child: StreamProvider<Model1>(
        create: (context) {
          return Stream.periodic(
              Duration(seconds: 2), (data) => Model1()..count = data);
        },
        initialData: Model1(),
        builder: (context, child) =>
            Text('StreamProvider count:${Provider.of<Model1>(context).count}'),
      ),
    );
  }
}

class _ProxyProviderWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 24.0),
      child: ProxyProvider<Model1, User>(
        update: (context, value, previous) =>
            User('change value ${value.count}'),
        builder: (context, child) =>
            Text('ProxyProvider user:${Provider.of<User>(context).name}'),
      ),
    );
  }
}

class _FutureProviderWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 24.0),
      child: FutureProvider<Model1>(
        create: (context) {
          return Future.delayed(Duration(seconds: 2)).then((value) {
            print('future delayed...');
            Model1()..count = 11;
            return Model1();
          });
        },
        initialData: Model1(),
        builder: (context, child) =>
            Text('FutureProvider count:${Provider.of<Model1>(context).count}'),
      ),
    );
  }
}

class _ConsumerWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 24.0),
      child: Consumer<Model1>(builder: (context, value, child) {
        return Text('Consumer count:${value.count}');
      }),
    );
  }
}

class _Select2Widget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 24.0),
      child: Selector2<Model1, Model2, int>(
        builder: (context, totalCount, child) =>
            Text('selector2 total count:$totalCount'),
        selector: (context, model1, model2) => model1.count + model2.count,
        shouldRebuild: (previous, next) => true,
      ),
    );
  }
}

class _SelectWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 24.0),
      child: Selector<Model1, int>(
        builder: (context, count, child) => Text('selector count:$count'),
        selector: (context, model) => model.count,
      ),
    );
  }
}

class _ValueNotifierWidget extends StatelessWidget {
  final ValueNotifier<int> _counter = ValueNotifier<int>(0);

  @override
  Widget build(BuildContext context) {
    _counter.value = Provider.of<Model1>(context).count;
    return Padding(
      padding: const EdgeInsets.only(top: 24.0),
      child: ValueListenableBuilder(
        valueListenable: _counter,
        builder: (context, count, child) =>
            Text('ValueListenableBuilder count:$count'),
      ),
    );
  }
}

class _AddButtonWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        // Provider.of<Model1>(context).count = DateTime.now().microsecondsSinceEpoch;
        Provider.of<Model1>(context, listen: false).count++;
      },
      child: Text('add'),
    );
  }
}

class _ShowTextWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text('count:${Provider.of<Model1>(context).count}');
  }
}

class Model1 extends ChangeNotifier {
  int _count = 0;

  int get count => _count;

  set count(int value) {
    _count = value;
    notifyListeners();
  }
}

class Model2 extends ChangeNotifier {
  int _count = 0;

  int get count => _count;

  set count(int value) {
    _count = value;
    notifyListeners();
  }
}

class User {
  var name;

  User(this.name);
}
